import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_application_cyber/model/cn_net_response.dart';
import 'package:flutter_application_cyber/net/cn_net_helper.dart';
import 'package:flutter_application_cyber/pages/Mine/Models/cn_BlockListModel.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_util_code/toast_utils.dart';
import 'package:flutter_application_cyber/pages/Mate/Models/cn_AnchorViewModel.dart';
import 'package:flutter_application_cyber/model/cn_follow_model.dart';
import '../../tools/cn_event_bus.dart';
import 'dart:async';

class CnFollowListPage extends StatefulWidget {
  const CnFollowListPage({super.key});
  @override
  State<CnFollowListPage> createState() => _CnFollowListPage();
}

class _CnFollowListPage extends State<CnFollowListPage> {
  List<CnFollowModel> _followList = [];
  String showDataStatus = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: _buildContext());
  }

  Widget _buildContext() {
    switch (showDataStatus) {
      case 'noData':
        return Center(
          child: Text('No noData'),
        );
      case 'data':
        return Column(
          children: <Widget>[
            Expanded(
                child: Stack(
              children: [
                Container(
                  constraints: BoxConstraints(maxHeight: 1000),
                  child: ListView.builder(
                      scrollDirection: Axis.vertical, // 滚动方向
                      itemCount: _followList.length,
                      itemBuilder: (context, index) {
                        CnFollowModel model = _followList[index];
                        return Container(
                            height: 86,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Row(
                                  children: [
                                    Container(
                                      margin:
                                          EdgeInsets.only(left: 16, right: 10),
                                      width: 46,
                                      height: 46,
                                      clipBehavior: Clip.hardEdge,
                                      decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(23)),
                                      child: Image.network(
                                        model.avatarUrl,
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                    Text(
                                      getSubString(end: 8, str: model.nickname),
                                      style: const TextStyle(
                                          fontSize: 16,
                                          color: Color(0xFF202020)),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ],
                                ),
                                InkWell(
                                  onTap: () {
                                    // if (!ClickUtils.isClick()) return;
                                    EasyLoading.show(status: 'loading...');
                                    CnAnchorViewModel.getInfo().follow(
                                        model.userId, true, (bool value) {
                                      getData();
                                      EasyLoading.dismiss();
                                    });
                                  },
                                  child: Container(
                                      margin: const EdgeInsets.only(right: 16),
                                      alignment: Alignment.center,
                                      width: 72,
                                      height: 32,
                                      decoration: const BoxDecoration(
                                          image: DecorationImage(
                                              image: AssetImage(
                                                  'images/icon_btn_bg_comment.png'),
                                              fit: BoxFit.fill)),
                                      child: const Text(
                                        'Followed',
                                        style: TextStyle(
                                          color: Color(0xFFFFFFFF),
                                          fontSize: 12,
                                        ),
                                        textAlign: TextAlign.center,
                                      )),
                                ),
                              ],
                            ));
                      }),
                )
              ],
            ))
          ],
        );
      default:
        return Center(
          child: CircularProgressIndicator(
            color: Colors.white,
          ),
        );
    }
  }

  @override
  void initState() {
    super.initState();
    getData();
    CnEventBus.instance!.addListener(EventKeys.FollowListPageMessage, (arg) {
      getData();
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    CnEventBus.instance!.removeListener(EventKeys.FollowListPageMessage);
  }

  void getData() {
    CnAnchorViewModel.getInfo().getFollowPagingList(20, 1,
        (List<dynamic> followList) {
      _followList.clear();
      if (followList.isNotEmpty) {
        if (mounted) {
          setState(() {
            _followList = followList as List<CnFollowModel>;
            showDataStatus = 'data';
          });
        }
      } else {
        if (mounted) {
          setState(() {
            showDataStatus = 'noData';
          });
        }
      }
    });
  }
}

typedef FunctionTest<String> = void Function(String isSure);
//字符串截取
String getSubString({required int end, required String str}) {
  if (str.length > end) {
    return "${str.substring(0, end)}...";
  } else {
    return str;
  }
}
